package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.dialog.ApLinkAlertDialog;
import com.aplink.generic.dialog.ApLinkProgressDialog;
import com.aplink.generic.dialog.DialogButton;
import com.aplink.generic.dialog.DialogInfo;
import com.aplink.generic.dialog.SparseDialog;
import com.aplink.generic.util.ResourcesHelper;

public class DialogParser {
    public static final String ACTION = "action";
    public static final String ALERT_DIALOG = "alertDialog";
    public static final String BROADCAST_STATUS = "broadcastStatus";
    public static final String BUTTON_NEGATIVE = "button_negative";
    public static final String BUTTON_NEUTRAL = "button_neutral";
    public static final String BUTTON_POSITIVE = "button_positive";
    public static final String CANCELABLE = "Cancelable";
    public static final String CANCELABLE_ON_TOUCH_OUTSIDE = "CanceledOnTouchOutside";
    public static final String DIALOG_INFO = "dialogInfo";
    public static final String IS_FINISH_ACTIVITY = "isFinishActivity";
    public static final String IS_FORMAT_HTML = "isFormatHTML";
    public static final String IS_LINK_TO_NEXT_PAGE = "isLinkToNextPage";
    public static final String MESSAGE = "message";
    public static final String PROGRESS_BAR = "progressDialog";
    public static final String STATUS = "status";
    public static final String TITLE = "title";

    private static String getViewTitile(final String viewTitle) {
        return ResourcesHelper.getStringByName(viewTitle);
    }

    public static SparseDialog parseApLinkAlertDialog(Node node) {
        final SparseDialog alertDialogs = new SparseDialog();
        Element element = (Element) node;
        final NodeList nodes = element
                .getElementsByTagName(DialogParser.ALERT_DIALOG);
        if (nodes.getLength() == 0) {
            return alertDialogs;
        }
        for (int item = 0; item < nodes.getLength(); item++) {
            final ApLinkAlertDialog apLinkAlertDialog = new ApLinkAlertDialog();
            node = nodes.item(item);
            int actionCode = -1;
            if (node.hasAttributes()) {
                String value = XMLPaserUtil.getAttrValue(node,
                        DialogParser.CANCELABLE);
                if (!TextUtils.isEmpty(value) && value.equalsIgnoreCase("Y")) {
                    apLinkAlertDialog.setCancelable(true);
                }

                value = XMLPaserUtil.getAttrValue(node,
                        DialogParser.CANCELABLE_ON_TOUCH_OUTSIDE);
                if (!TextUtils.isEmpty(value) && value.equalsIgnoreCase("Y")) {
                    apLinkAlertDialog.setCanceledOnTouchOutside(true);
                }

                value = XMLPaserUtil.getAttrValue(node, DialogParser.ACTION);
                if (!TextUtils.isEmpty(value)) {
                    actionCode = ActionType.get(value).getCode();
                }
            }
            element = (Element) node;
            final NodeList dialogInfoList = element
                    .getElementsByTagName(DialogParser.DIALOG_INFO);
            for (int i = 0; i < dialogInfoList.getLength(); i++) {

                final Node nodeDialog = dialogInfoList.item(i);
                String dialogKey = null;
                if (nodeDialog.hasAttributes()) {
                    dialogKey = XMLPaserUtil.getAttrValue(nodeDialog,
                            DialogParser.STATUS);
                }
                if (nodeDialog.getNodeType() == Node.ELEMENT_NODE) {
                    final DialogInfo dialogInfo = DialogParser
                            .parseDialogInfo(nodeDialog);
                    apLinkAlertDialog.put(dialogKey, dialogInfo);
                }
            }
            alertDialogs.put(actionCode, apLinkAlertDialog);
        }
        return alertDialogs;
    }

    public static ApLinkProgressDialog parseApLinkProgressDialog(Node node) {
        final ApLinkProgressDialog apLinkProgressDialog = new ApLinkProgressDialog();
        Element element = (Element) node;
        final NodeList nodes = element
                .getElementsByTagName(DialogParser.PROGRESS_BAR);
        if (nodes.getLength() == 0) {
            return null;
        }

        node = nodes.item(0);

        if (node.hasAttributes()) {
            final String canceble = XMLPaserUtil.getAttrValue(node,
                    DialogParser.CANCELABLE);
            if ((canceble != null) && canceble.equalsIgnoreCase("Y")) {
                apLinkProgressDialog.setCancelable(true);
            }

            final String canceledOnTouchOutside = XMLPaserUtil.getAttrValue(
                    node, DialogParser.CANCELABLE_ON_TOUCH_OUTSIDE);
            if ((canceledOnTouchOutside != null)
                    && canceledOnTouchOutside.equalsIgnoreCase("Y")) {
                apLinkProgressDialog.setCanceledOnTouchOutside(true);
            }
        }
        element = (Element) node;
        final NodeList dialogInfoList = element
                .getElementsByTagName(DialogParser.DIALOG_INFO);

        for (int i = 0; i < dialogInfoList.getLength(); i++) {

            final Node nodeDialog = dialogInfoList.item(i);
            String dialogKey = null;
            if (nodeDialog.hasAttributes()) {
                dialogKey = XMLPaserUtil.getAttrValue(nodeDialog,
                        DialogParser.STATUS);
            }
            if (nodeDialog.getNodeType() == Node.ELEMENT_NODE) {
                final DialogInfo dialogInfo = DialogParser
                        .parseDialogInfo(nodeDialog);
                apLinkProgressDialog.put(dialogKey, dialogInfo);
            }
        }

        return apLinkProgressDialog;
    }

    private static DialogButton parseDialogButton(final Node nodeDialogButton) {
        final DialogButton dialogButton = new DialogButton();
        if (nodeDialogButton.hasAttributes()) {
            final String isFinishActivity = XMLPaserUtil.getAttrValue(
                    nodeDialogButton, DialogParser.IS_FINISH_ACTIVITY);
            if ((isFinishActivity != null)
                    && isFinishActivity.equalsIgnoreCase("Y")) {
                dialogButton.setFinishActivity(true);
            }
            final String isLinkToNextPage = XMLPaserUtil.getAttrValue(
                    nodeDialogButton, DialogParser.IS_LINK_TO_NEXT_PAGE);
            if ((isLinkToNextPage != null)
                    && isLinkToNextPage.equalsIgnoreCase("Y")) {
                dialogButton.setLinkToNextPage(true);
            }

            final String broadCastStatus = XMLPaserUtil.getAttrValue(
                    nodeDialogButton, DialogParser.BROADCAST_STATUS);
            if (!TextUtils.isEmpty(broadCastStatus)) {
                final ActionType actionType = ActionType.get(broadCastStatus);
                dialogButton.setActionType(actionType);
            }
        }
        final String value = DialogParser.getViewTitile(XMLPaserUtil
                .getNodeValue(nodeDialogButton));
        dialogButton.setText(value);
        return dialogButton;
    }

    public static DialogInfo parseDialogInfo(Node nodeDialog) {
        final DialogInfo dialogInfo = new DialogInfo();
        if (nodeDialog.hasAttributes()) {
            final String isFormatHTML = XMLPaserUtil.getAttrValue(nodeDialog,
                    DialogParser.IS_FORMAT_HTML);
            if (!TextUtils.isEmpty(isFormatHTML)) {
                if (isFormatHTML.equalsIgnoreCase("Y")) {
                    dialogInfo.setFormatHTML(true);
                }
            }
        }
        if (nodeDialog.getNodeType() == Node.ELEMENT_NODE) {
            final Element element = (Element) nodeDialog;
            // Get progress bar title
            NodeList nodes = element.getElementsByTagName(DialogParser.TITLE);
            if (nodes.getLength() != 0) {
                nodeDialog = nodes.item(0);
                final String value = DialogParser.getViewTitile(XMLPaserUtil
                        .getNodeValue(nodeDialog));
                dialogInfo.setTitle(value);
            }

            // Get progress bar message
            nodes = element.getElementsByTagName(DialogParser.MESSAGE);
            if (nodes.getLength() != 0) {
                nodeDialog = nodes.item(0);
                final String value = DialogParser.getViewTitile(XMLPaserUtil
                        .getNodeValue(nodeDialog));
                dialogInfo.setText(value);
            }

            // Get progress bar BUTTON_POSITIVE
            nodes = element.getElementsByTagName(DialogParser.BUTTON_POSITIVE);
            if (nodes.getLength() != 0) {
                nodeDialog = nodes.item(0);
                final DialogButton dialogButton = DialogParser
                        .parseDialogButton(nodeDialog);
                dialogInfo.setPositiveButton(dialogButton);
            }

            // Get progress bar BUTTON_NEGATIVE
            nodes = element.getElementsByTagName(DialogParser.BUTTON_NEGATIVE);
            if (nodes.getLength() != 0) {
                nodeDialog = nodes.item(0);
                final DialogButton dialogButton = DialogParser
                        .parseDialogButton(nodeDialog);
                dialogInfo.setNegativeButton(dialogButton);
            }

            // Get progress bar BUTTON_NEUTRAL
            nodes = element.getElementsByTagName(DialogParser.BUTTON_NEUTRAL);
            if (nodes.getLength() != 0) {
                nodeDialog = nodes.item(0);
                final DialogButton dialogButton = DialogParser
                        .parseDialogButton(nodeDialog);
                dialogInfo.setNeutralButton(dialogButton);
            }
        }
        return dialogInfo;
    }
}
